package net.bwie.realtime.service.jtprealtimeservice.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.service.jtprealtimeservice.been.TrafficUvBeen;
import net.bwie.realtime.service.jtprealtimeservice.service.TrafficService;
import net.bwie.realtime.service.jtprealtimeservice.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/jtp/traffic")
public class TrafficController {
    static final String defaultDate = "1970-01-01";

    @Autowired
    TrafficService trafficService;

    @RequestMapping("/UVCount")
    public String getUVCount(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<TrafficUvBeen> trafficUvBeenList = trafficService.getUVCount(date);

        if(null == trafficUvBeenList || trafficUvBeenList.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                JSON.toJSONString(trafficUvBeenList)
                +"\n" +
                "}";
    }

    @RequestMapping("/PVCount")
    public String getPVCount(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<TrafficUvBeen> trafficPvBeenList = trafficService.getPVCount(date);

        List<String> name = trafficPvBeenList.stream().map(TrafficUvBeen::getName).collect(Collectors.toList());
        List<Integer> value = trafficPvBeenList.stream().map(TrafficUvBeen::getValue).collect(Collectors.toList());

        if(trafficPvBeenList.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": "+
                JSON.toJSONString(name)
                +",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"渠道来源\",\n" +
                "        \"data\": "+
                JSON.toJSONString(value)
                +"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
}
